﻿Imports Telerik.Web.UI
Imports System.Data
Imports System.Data.SqlClient
Partial Class Tools_Users
    Inherits System.Web.UI.Page

    Dim _CommandName As String

    Public strConnection As String = ConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString.ToString

    'Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

    '    Dim obj As New Authorization
    '    Dim url As String = Request.AppRelativeCurrentExecutionFilePath

    '    Dim id As String = obj.GetMenuID(url)
    '    Dim is_Authorized As Boolean = obj.CheckAuthorization(id)

    '    If is_Authorized = True Then
    '        'User Authorized to See that page .. Continue
    '    Else
    '        'User is not Authorized 
    '        Response.Redirect("~/UnAuthorized.aspx")
    '    End If


    'End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")
        End If
    End Sub

    Protected Sub Page_PreInit(sender As Object, e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub

    Protected Sub rgUsers_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgUsers.ItemCommand
        _CommandName = e.CommandName

        If _CommandName = "InitInsert" Then
            '"Add new" button clicked
            Dim editColumn As GridEditCommandColumn = DirectCast(rgUsers.MasterTableView.GetColumn("EditCommandColumn"), GridEditCommandColumn)
            'editColumn.Visible = False
            e.Canceled = True

            'Set the default value for the checkbox
            Dim newValues As New System.Collections.Specialized.ListDictionary()
            newValues("Admin") = False

            'Set the default value for the drop down list

            'newValues["yourDropDownList"] = "Active";

            e.Item.OwnerTableView.InsertItem(newValues)

        End If

    End Sub

    Protected Sub OnItemDataBoundHandler(ByVal sender As Object, ByVal e As GridItemEventArgs)
        If e.Item.IsInEditMode Then
            Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)

            If Not (TypeOf e.Item Is IGridInsertItem) Then
                Dim combo As RadComboBox = DirectCast(item.FindControl("rcbEmployee"), RadComboBox)

                Dim selectedItem As New RadComboBoxItem()
                selectedItem.Text = DirectCast(e.Item.DataItem, DataRowView)("emp_Num").ToString()
                selectedItem.Value = DirectCast(e.Item.DataItem, DataRowView)("emp_Num").ToString()
                selectedItem.Attributes.Add("emp_Num", DirectCast(e.Item.DataItem, DataRowView)("emp_Num").ToString())
                selectedItem.Attributes.Add("FName", DirectCast(e.Item.DataItem, DataRowView)("FName").ToString())

                combo.Items.Add(selectedItem)

                selectedItem.DataBind()

                Session("emp_Num") = selectedItem.Value

                'ElseIf (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) And _CommandName = "InitInsert" Then
                '    Dim InsertFormItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
                '    If IsDBNull(CType(InsertFormItem.FindControl("chkIsAdmin"), CheckBox)) Then
                '        Convert.ToBoolean(CType(InsertFormItem.FindControl("chkIsAdmin"), CheckBox))
                'End If

            End If

        End If


    End Sub

    Protected Sub rcbEmployee_ItemsRequested(ByVal sender As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)

        Dim sql As String = "SELECT isnull([emp_Num],0) as [emp_Num], isnull([FName],'-') as [FName] FROM [Employee] where (emp_Num like @emp_Num + '%' or  @emp_Num ='')  or (FName like @FName + '%' or  @FName ='')"
        Dim adapter As New SqlDataAdapter(sql, strConnection)

        adapter.SelectCommand.Parameters.AddWithValue("@FName", e.Text)
        adapter.SelectCommand.Parameters.AddWithValue("@emp_Num", e.Text)

        Dim dt As New DataTable()
        adapter.Fill(dt)

        Dim comboBox As RadComboBox = DirectCast(sender, RadComboBox)
        ' Clear the default Item that has been re-created from ViewState at this point.
        comboBox.Items.Clear()

        For Each row As DataRow In dt.Rows
            Dim item As New RadComboBoxItem()
            item.Text = row("emp_Num").ToString()
            item.Value = row("emp_Num").ToString()
            item.Attributes.Add("emp_Num", row("emp_Num").ToString())
            item.Attributes.Add("FName", row("FName").ToString())

            comboBox.Items.Add(item)

            item.DataBind()
        Next
    End Sub

    Protected Sub OnSelectedIndexChangedHandler(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
        Session("emp_Num") = e.Value
    End Sub
End Class
